package model;

import persistency.controller.RegistrationController;


public class OfferedCourseOpenState implements OfferedCourseState {

	private OfferedCourse offeredCourse;

	public OfferedCourseOpenState(OfferedCourse offeredCourse) {
		this.offeredCourse = offeredCourse;
	}

	@Override
	public boolean registerStudent(Registration registration) {
		boolean result = RegistrationController.instance().insertRegistration(registration);
		if (RegistrationController.instance().getByOfferedCourseIdRegistrations(offeredCourse.getId()).size() >= offeredCourse.getNumberOfPlaces()) {
			offeredCourse.setState(offeredCourse.getOfferedCourseFullState());
		}
		return result;
	}

	@Override
	public void unregisterStudent(Registration registration) {
		if (RegistrationController.instance().getByOfferedCourseIdRegistrations(offeredCourse.getId()).size() > 0) {
			RegistrationController.instance().deleteRegistration(registration.getId());
		}

	}

	@Override
	public void closeRegistration() {
		if(RegistrationController.instance().getByOfferedCourseIdRegistrations(offeredCourse.getId()).size() >= offeredCourse.getMinNumberOfPlaces()){
			offeredCourse.setState(offeredCourse.getOfferedCourseClosedState());
		}else{
			offeredCourse.setState(offeredCourse.getOfferedCourseCanceledState());
		}

	}

	@Override
	public void cancelOfferedCourse() {
		offeredCourse.setState(offeredCourse.getOfferedCourseCanceledState());

	}
	
	@Override
	public String toString(){
		return model.enums.OfferedCourseState.OPEN.toString();
	}

}
